//
//  ViewController.m
//  ControllFun
//
//  Created by student on 16/2/18.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userText;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UILabel *sliderLabel;
@property (weak, nonatomic) IBOutlet UISwitch *leftSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *rightSwitch;
@property (weak, nonatomic) IBOutlet UIButton *dosometingBtn;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIImage *image1 = [UIImage imageNamed:@"whiteButton"];
    UIImage *image2 = [UIImage imageNamed:@"blueButton"];
    //创建拉伸图片
    UIEdgeInsets insets = UIEdgeInsetsMake(0, 12, 0, 12);
    UIImage *image3 = [image1 resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    UIImage *image4 = [image2 resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    
    [self.dosometingBtn setBackgroundImage:image3 forState:UIControlStateNormal];
    [self.dosometingBtn setBackgroundImage:image4 forState:UIControlStateHighlighted];
    //代码改状态？
    //self.dosometingBtn.buttonType = UIButtonTypeCustom;
}
//改变slider的值 在label中显示
- (IBAction)changeSlider:(UISlider *)sender {
    int num = sender.value;
    self.sliderLabel.text = [NSString stringWithFormat:@"%i",num];
}
//改变segment的值 显示与隐藏switch 和button的状态
- (IBAction)selectSegment:(UISegmentedControl *)sender {
//    @property(nonatomic) NSInteger selectedSegmentIndex;
    
    if (sender.selectedSegmentIndex == 0) {
        self.dosometingBtn.hidden = YES;
        self.leftSwitch.hidden = NO;
        self.rightSwitch.hidden = NO;
    }
    else if(sender.selectedSegmentIndex == 1){
        self.dosometingBtn.hidden = NO;
        self.leftSwitch.hidden = YES;
        self.rightSwitch.hidden = YES;
    }
}
//点击左右两边的switch 跟随动作
- (IBAction)didClickSwitch:(UISwitch *)sender {
//    @property(nonatomic,getter=isOn) BOOL on;
//    
//    - (instancetype)initWithFrame:(CGRect)frame NS_DESIGNATED_INITIALIZER;      // This class enforces a size appropriate for the control, and so the frame size is ignored.
//    - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
//    
//    - (void)setOn:(BOOL)on animated:(BOOL)animated; // does not send action
    BOOL open = sender.on;
    [self.leftSwitch setOn:open animated:YES];
    [self.rightSwitch setOn:open animated:YES];
    
}
//触摸屏幕把键盘放下去
- (IBAction)hiddenKeyboard:(UIControl *)sender {
    [self.view endEditing:YES];
}
- (IBAction)didClickdosomethingBtn:(UIButton *)sender {
    //创建alertController
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"你确定吗" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //添加按钮
    NSString *str1 = [NSString stringWithFormat:@"%@,放轻松,一切都正常运行!",self.userText.text];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"是的,我确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"某些事项被执行了" message:str1 preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"欧,耶" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action4];
        [self presentViewController:alert animated:YES completion:nil];
    }];
    [alertController addAction:action1];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:nil];
    [alertController addAction:action2];
    
    
    //显示alertController
    [self presentViewController:alertController animated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
